package org.javabasics.other;

/**
 * Created by LUOLIANG on 2016/8/30.
 */
public class SingletonTest {
    public static void main(String[] args) {
        Singleton s1 = Singleton.getInstance();
        Singleton s2 = Singleton.getInstance();

        System.out.println(s1 == s2);
    }
}

class Singleton {
    //使用一个类变量来缓存曾经创建的实例
    private static Singleton instance;

    //隐藏构造器
    private Singleton() {
    }

    //提供一个静态方法用于返回Singleton
    public static Singleton getInstance() {
        if (instance == null)
            instance = new Singleton();

        return instance;
    }
}